﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using System;
using System.IO;

namespace CatUtils.Utils.SpecialUtils.ContentRecognition.BaiduUtils.Ocr
{
    internal class BaiduOcrhelper
    {
        /// <summary>
        /// API_KEY保存路径
        /// </summary>
        public readonly static string API_KEY_Path = StaticValue.BinPath + "OcrAPIKEY.txt";

        /// <summary>
        /// SECRET_KEY保存路径
        /// </summary>
        public readonly static string SECRET_KEY_Path = StaticValue.BinPath + "OcrSECRETKEY.txt";

        public static string ReadFile(string FilePath)
        {
            try
            {
                var client = new Baidu.Aip.Ocr.Ocr(FileHelper.ReadAllText(API_KEY_Path), FileHelper.ReadAllText(SECRET_KEY_Path));
                client.Timeout = 60000;  // 修改超时时间
                var image = File.ReadAllBytes(FilePath);
                // 调用通用文字识别, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.AccurateBasic(image);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        public static string ReadIDCardFile(string FilePath)
        {
            try
            {
                var client = new Baidu.Aip.Ocr.Ocr(FileHelper.ReadAllText(API_KEY_Path), FileHelper.ReadAllText(SECRET_KEY_Path));
                client.Timeout = 60000;  // 修改超时时间
                var image = File.ReadAllBytes(FilePath);
                // 调用通用文字识别, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
                var idCardSide = "front";

                // 调用身份证识别，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.Idcard(image, idCardSide);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }
    }
}